package jp.sourceforge.akjrcp.dropsomething2;



import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;

public class HistoryListTable extends TableViewer{
	protected List<HistoryData> dataList=new ArrayList<HistoryData>();
	public HistoryListTable(Composite parent, int style) {
		super(parent, style);
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		setInput(dataList);
		
		
	    getTable().setHeaderVisible(false);
	    
	    TableColumn column = new TableColumn(getTable(),SWT.NONE);
	    column.setText("status");
	    column.setWidth(80);
	    
	    column = new TableColumn(getTable(),SWT.NONE);
	    column.setText("time");
	    column.setWidth(120);
	    column = new TableColumn(getTable(),SWT.NONE);
	    
	    column.setWidth(400);
	 
	   
	}

	public void clear(){
		dataList.removeAll(dataList);
		refresh();
	}

	public void addHistory(HistoryData history){
		dataList.add(0,history);
		refresh();
	}
	
    class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
			
		}
		public void dispose() {
		}
		public Object[] getElements(Object parent) {
			
			return dataList.toArray();
		}
	}
    
    private SimpleDateFormat format=new SimpleDateFormat("MM/dd hh:mm:ss");
	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			
			HistoryData history=((HistoryData)obj);
			if(index==0){
				return history.getStatus();
			}else if(index==1){
				return format.format(new Date(history.getTime()));
			}else if(index==2){
				return history.getLabel();
			}
			return "";
		}
		public Image getColumnImage(Object obj, int index) {
			return null;
		}

	}
}
